package me.luger.project.fof.service;

import me.luger.core.commom.util.DateUtil;
import me.luger.core.dao.nutz.service.BaseService;
import me.luger.project.fof.config.cache.redis.RedisManager;
import me.luger.project.fof.entity.FofCollect;
import org.nutz.dao.Dao;
import org.nutz.dao.Sqls;
import org.nutz.dao.sql.Sql;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository
public class FofCollectService extends BaseService<FofCollect> {
    @Autowired
    private RedisManager redisManager;

    public FofCollectService(Dao dao) {
        super(dao);
    }

    public boolean collect(Integer uid,Integer fofId){
        FofCollect collect = new FofCollect();
        collect.setUid(uid);
        collect.setFofId(fofId);
        collect = this.insert(collect);
        if(collect.getId() != null){
            Double now = DateUtil.nowAsDate().getTime() + 0D;
            redisManager.zadd("fof:user:fof:collect:"+uid,now,String.valueOf(fofId));
            return true;
        }
        return false;
    }

    public boolean unCollect(Integer uid,Integer fofId){
        Sql delSql = Sqls.create("select * from fof_collect where uid = @uid and fofId = @fofId");
        delSql.setParam("uid",uid);
        delSql.setParam("fofId",fofId);
        FofCollect collect = this.fetchOne(delSql);
        if(null == collect){
            return false;
        }
        this.delete(new Integer[]{collect.getId()});
        redisManager.zrem("fof:user:fof:collect:"+uid,String.valueOf(fofId));
        return true;
    }

    public boolean isCollect(Integer uid, Integer fofId) {
        Double collectTime = redisManager.zscore("fof:user:fof:collect:"+uid,String.valueOf(fofId));
        if(null != collectTime && collectTime > 0){
            return true;
        }
        return false;
    }
}
